﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AcsCommon;

namespace YiShi.KnowledgePoint
{
    public partial class KnowledgePointTree : System.Web.UI.UserControl
    {
        public enum SelectType { radio, checkbox }
        DbAccess db;
        string buttonTemplate = @"
<button name='ExpandButton' style='font-size: 0pt;'>
    <span class='ui-icon {0}'></span>
</button>";
        string trTemplate = @"
<tr {0} align='left' style='display:{1}' level='{2}' sortindex='{3}' type='{4}'>
    <td class='single-border' style='width: 20px;'>
        <input name='Point' type='{5}' value='{6}' />
    </td>
    <td style='font-weight: bold;'>
        <span name='BlankSpan'>{7}</span>
        {8}
        <span name='NameSpan' style='color:{9}'>{10}</span>
    </td>
</tr>";
        protected string tr;
        protected string html;
        public SelectType Type { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public KnowledgePointTree()
        {
            db = DbAccess.Get("Admin");
            Type = SelectType.radio;
            Width = 0;
            Height = 0;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            tr = string.Format(trTemplate, "id='TrTemplate'", "none", "", "", "", Type, "", "", "", "Black", "");
            Class.KnowledgePoint kp = Class.KnowledgePoint.GetRoot(db);
            html = GetHtml(kp, 0);
        }
        private string GetHtml(YiShi.Class.KnowledgePoint kp, int level)
        {
            string blank = "";
            for (int i = 0; i < level; i++)
                blank += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

            Class.KnowledgePoint[] kps = kp.GetChild();
            string str = string.Format(trTemplate, "", "", level, kp.SortIndex, kp.Type, Type, kp.Id, blank, kps.Length > 0 ? string.Format(buttonTemplate, "ui-icon-minusthick") : "", kp.Type=="0"?"Black":"Gray", kp.Name);
            for (int i = 0; i < kps.Length; i++)
                str += GetHtml(kps[i], level + 1);
            return str;
        }
    }
}